﻿#region
/*******************************************************************************
** File Name:		    CategoryService.cs
** Creator:		        chao.jiang 
** EMail:               chao.jiang 
** Create date:	        
** Latest Modifier:
** Latest Modify date:
** Description:		    
** Modify History:
**
** Code Review:
**
** Version number:       1.0.0
*********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.IBatisRepository;
using Phoenix.Respository;
using System.Data;
using Phoenix.Entity;
using System.Collections;

namespace Phoenix.Service
{
    public class CategoryService : ICategoryService
    {
		 ICategory category = new CategoryRepository();
		 
		 #region  Auto Generation
		 
        public void AddCategory(CategoryModel model)
        {
			ECategory entity = ConvertModelToEntity(model);
			
            category.AddCategory(entity);
        }

		public void UpdateByCategoryID (CategoryModel model)
		{
			ECategory entity = ConvertModelToEntity(model);

            category.UpdateByCategoryID(entity);

		}

		public void DeleteByCategoryID (System.Int32 categoryID)
		{
		
            category.DeleteByCategoryID(categoryID);

		}
		 
        public CategoryModel GetByCategoryID (System.Int32 categoryID)
        {
        
			ECategory entity = category.GetByCategoryID(categoryID);
			
            CategoryModel model = ConvertEntityToModel(entity);


            return model;
        }
        
	
		public IList<CategoryModel> GetAll ()
		{
			IList<ECategory> entity = category.GetAll();
			
			if (entity == null)
            {
                return null;
            }
            
            IList<CategoryModel> list = new List<CategoryModel>();

            foreach (var item in entity)
            {
                CategoryModel model = ConvertEntityToModel(item);
                list.Add(model);
            }
            return list;   
			
		}
		
		private CategoryModel ConvertEntityToModel(ECategory entity)
		{
			if (entity == null)
            {
                return null;
            }
			CategoryModel model = new CategoryModel();
                        model.CategoryID = entity.CategoryID;
                        model.CategoryName = entity.CategoryName;
                        model.ParentId = entity.ParentId;
                        model.CateAleph = entity.CateAleph;
                        model.CateStatus = entity.CateStatus;
                        model.IDPath = entity.IDPath;
                        model.NamePath = entity.NamePath;
                        return model;
		}
		
		private ECategory ConvertModelToEntity(CategoryModel model)
		{
			if (model == null)
            {
                return null;
            }
			ECategory entity = new ECategory();
                        entity.CategoryID = model.CategoryID;
                        entity.CategoryName = model.CategoryName;
                        entity.ParentId = model.ParentId;
                        entity.CateAleph = model.CateAleph;
                        entity.CateStatus = model.CateStatus;
                        entity.IDPath = model.IDPath;
                        entity.NamePath = model.NamePath;
                        return entity;
		}
		
		
		
		#endregion

		public IList<CategoryModel> GetByParentID(int pid)
		{
			IList<ECategory> entity = category.GetByParentID(pid);

			if (entity == null)
			{
				return null;
			}

			IList<CategoryModel> list = new List<CategoryModel>();

			foreach (var item in entity)
			{
				CategoryModel model = ConvertEntityToModel(item);
				list.Add(model);
			}
			return list;   
		}


    }
}